function linkage(){}  
 
$(function(){
    //linkage.init("state","country","city");  
});  
 
 
linkage.init = function(levelOne, levelTwo, levelThree){  
 
    linkage.data = [];  
 
    levelOne = "#" + levelOne;  
    levelTwo = "#" + levelTwo;  
    levelThree = "#" + levelThree;  
    var html = "";  
    var url = "division.vm";//访问地址  
      
    //JQuery访问数据  
    $.getJSON(url, function(data){  
        linkage.data =data.provs;  
        $(levelOne).html(linkage.getOptionHtmlForLocation());//地区信息
        $(levelTwo).html(linkage.getOptionHtmlByIdForUniversity($(levelOne).val()));//高校信息
        $(levelThree).html(linkage.getOptionHtmlByIdForDept($(levelTwo).val()));//专业信息
          
        // 为下拉levelOne框绑定onchange事件  
        $(levelOne).change(function(){  
            $(levelTwo).html(linkage.getOptionHtmlByIdForUniversity($(levelOne).val()));//高校信息
            $(levelThree).html(linkage.getOptionHtmlByIdForDept($(levelTwo).val()));//专业信息
        });  
        // 为下拉levelTwo框绑定onchange事件  
        $(levelTwo).change(function(){  
            $(levelThree).html(linkage.getOptionHtmlByIdForDept($(levelTwo).val()));//专业信息
        });  
          
    });  
};  
 
/**  
 * 获得下拉框的选项html  
 * @param data   
 * @return html  
 */ 
linkage.getOptionHtml = function(data,type){  
    var html = "";  
    if(type != 0){
    	 html = "<option value="+data.id+">" + data.name + "</option>";  
    }else{
    	 html = "<option value="+data+">" + data + "</option>";  
    }
   
    return html;  
};  

/**
 * 院系数据
 */
linkage.getOptionHtmlByIdForDept = function(subId) {
	var html = "";
	for (i = 0; i < linkage.data.length; i++) {
		for (j = 0; j < linkage.data[i].univs.length; j++) {
			if (linkage.data[i].univs[j].id == subId) {
				for (k = 0; k < linkage.data[i].univs[j].depts.length; k++) {
					html += linkage.getOptionHtml(linkage.data[i].univs[j].depts[k],0);
				}
			}
		}
	}
	return html;
}; 

/**
 * 高校数据
 */
linkage.getOptionHtmlByIdForUniversity = function(subId){ 
    var html = "";  
    for(i = 0 ; i < linkage.data.length ; i++){
        if(linkage.data[i].id == subId){  
        	for(j = 0 ; j < linkage.data[i].univs.length ; j++){
               html += linkage.getOptionHtml(linkage.data[i].univs[j]);  
            }  
        }  
    }  
    return html;  
}; 
 
/**  
 * 地区数据
 */ 
linkage.getOptionHtmlForLocation = function(){  
    var html = "";  
    for(i = 0 ; i < linkage.data.length ; i++){  
    	html += linkage.getOptionHtml(linkage.data[i]);  
    }  
    return html;  
}; 